<?php
class Exam_QuestionController extends Core_Controller_Action_Standard {
    public function init() {
        if( !$this->_helper->requireAuth()->setAuthParams('exam', null, 'view')->isValid() ) return;

        else if( 0 !== ($question_id = (int) $this->_getParam('question_id')) &&
                null !== ($question = Engine_Api::_()->getItem('exam_question', $question_id)) ) {
            Engine_Api::_()->core()->setSubject($question);
        }
    }
    public function editAction() {
        if( !$this->_helper->requireUser()->isValid() ) return;
        if( !$this->_helper->requireSubject('exam_question')->isValid() ) return;
        $this->view->question = $question = Engine_Api::_()->core()->getSubject();
        $this->view->exam = $exam = Engine_APi::_()->getItem('exam', $this->_getParam('exam_id'));
        if( !$this->_helper->requireAuth()->setAuthParams($exam, null, 'edit')->isValid() ) return;
        $this->view->isSuccess = false;
        $viewer = Engine_Api::_()->user()->getViewer();
        $this->view->tinyMce()->setOptions(Engine_Api::_()->exam()->initTinyMCE());
        $this->view->form = $form = new Exam_Form_Exam_Addquestion();
        $form->setTitle('Edit Question');
        $form->setDescription('');
        $form->submit->setLabel(Zend_Registry::get('Zend_Translate')->_('Save Changes'));

        if( !$this->getRequest()->isPost() ) {
            $form->populate($question->toArray());
            $this->view->answers = $answers = Engine_Api::_()->getItemTable('exam_answer')->getAnswersByQuestion($question);

            return;
        }
        if( !$form->isValid($this->getRequest()->getPost()) ) {
            return;
        }
        // Process
        $values = $this->getRequest()->getPost();

        $values['category'] = 2;
        $db = $question->getTable()->getAdapter();

        $db->beginTransaction();
        try {
            $question->setFromArray($values);
            $question->save();

            /*
            //delete all answers
            $answerTbl =  Engine_Api::_()->getItemTable('exam_answer');
            $where = $answerTbl->getAdapter()->quoteInto('question_id = ?', $question->getIdentity());
            $answerTbl->delete($where);

            //add answers
            $number_choice = 0;
            $answer_order = 0;
            foreach( $values['answer'] as $key=>$value) {
                //create new info
                $answer = $answerTbl->createRow();
                $answer->question_id = $question->getIdentity();
                $answer->content = $value;
                $answer->explanation = '';
                $answer->order = ++$answer_order;
                if(isset($values['answer_correct_'. $key]) && !empty($values['answer_correct_'. $key])) {
                    $answer->result = 1;
                    $number_choice++;
                }else {
                    $answer->result = 0;
                }
                $answer->save();
            };

            //update $number_choice
            $question->number_choice = $number_choice;
             * 
             */
            $question->save();
            $db->commit();

        }catch (Exception $e) {
            $db->rollBack();
//  		throw $e;
        }
    }
}
